﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using Acme.Sakila.EFDAL;
using Acme.Sakila.EFDAL.Entity;

namespace Acme.Sakila.TestSite
{
	public partial class RentalItem : BasePersistablePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cmdSave.Click += new EventHandler(SaveData);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
		}

		#endregion

		#region Methods

		protected override nHydrate.EFCore.DataAccess.NHEntityObject CreateObject(SakilaEntities context)
		{
			var newItem = new Acme.Sakila.EFDAL.Entity.rental();
			context.AddItem(newItem);
			return newItem;
		}

		protected override void SetupBindings(SakilaEntities context)
		{
			var rentalId = this.Request["id"].ToInt32();
			var theRental = context.rental.FirstOrDefault(x => x.rental_id == rentalId);
			if (theRental == null)
				theRental = CreateObject(context) as Acme.Sakila.EFDAL.Entity.rental;

			Mapper.Map(lblOrderId, theRental, Acme.Sakila.EFDAL.Entity.rental.FieldNameConstants.rental_id);
			Mapper.Map(txtRentalDate, theRental, Acme.Sakila.EFDAL.Entity.rental.FieldNameConstants.rental_date, lblRentalDate);
			Mapper.Map(txtReturnDate, theRental, Acme.Sakila.EFDAL.Entity.rental.FieldNameConstants.return_date, lblReturnDate);

			#region Customer
			linkCustomer.Text = "(NOT SET)";
			linkCustomer.NavigateUrl = "#";
			if (theRental.customer != null)
			{
				linkCustomer.Text = theRental.customer.ToString();
				linkCustomer.NavigateUrl = "/CustomerItem.aspx?id=" + theRental.customer_id;
			}
			#endregion

		}

		#endregion

		#region Event Handlers

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.Response.Redirect(SessionHelper.LastRentalListSearch);
		}

		protected override void OnDataSavedSucess(EventArgs e)
		{
			base.OnDataSavedSucess(e);
			this.Response.Redirect(SessionHelper.LastRentalListSearch);
		}

		#endregion

	}
}